package com.zzld.modules.maintenance.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.zzld.core.utils.CookieUtils;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.maintenance.entity.Brand;
import com.zzld.modules.maintenance.entity.Matnproduct;
import com.zzld.modules.maintenance.service.IBrandService;
import com.zzld.modules.maintenance.service.IMatnexpenseService;
import com.zzld.modules.maintenance.service.IMatnexpensepathService;
import com.zzld.modules.maintenance.service.IMatnproductService;
import com.zzld.modules.maintenance.service.IPathService;
import com.zzld.modules.maintenance.entity.Path;
import com.zzld.modules.autoinn.entity.Area;
import com.zzld.modules.autoinn.service.IAreaService;

@Controller
@RequestMapping("/weixin/f/matenance/by/brand")
public class WXMtanController {

	private String carlist = "/wx/matenance/carList";
	private String index = "/wx/matenance/index";
	private String citylist = "/wx/matenance/cityList";
	private String serlist = "/wx/matenance/serviceList";
	
	private String ero="/wx/matenance/erro404";

	@Autowired
	private IAreaService areaService;

	@Autowired
	private IBrandService brandService;

	@Autowired
	private IMatnproductService productService;

	@Autowired
	private IPathService pathService;

	@Autowired
	private IMatnexpenseService expenseService;

	@Autowired
	private IMatnexpensepathService expensepathService;

	@RequestMapping("carlist")
	public String list() {
		return carlist;
	}

	@RequestMapping("/service/{id}")
	public String service(Model model,HttpServletRequest request,HttpServletResponse response,
			@PathVariable("id") String id){
		String city="";
		String baranId="";
	    
		try{
			baranId=CookieUtils.getCookie(request, "Brand_session");
	    	city = 	CookieUtils.getCookie(request, "str");
	    	if(city!=null){
	    		if(city.indexOf("区")>-1){
	    			model.addAttribute("city", city);
	    		}else{
	    			model.addAttribute("city", areaService.selectbyid(city).getAreaname());
	    		}
	    	}
	    	String di="";
	    	if(StringUtils.isNotBlank(baranId)){
	    		Example e = new Example(Brand.class);
	    		e.createCriteria().andEqualTo("brandId", baranId);
	    		String ids  = brandService.selectByExample(e).get(0).getPid();
	    		
	    		Example e1 = new Example(Brand.class);
	    		e1.createCriteria().andEqualTo("brandId", ids);
	    		String idw  = brandService.selectByExample(e1).get(0).getPid();
	    		
	    		
	    		Example e2 = new Example(Brand.class);
	    		e2.createCriteria().andEqualTo("brandId", idw);
	    		di+=brandService.selectByExample(e2).get(0).getBrandname()+" ";
	    		di+=brandService.selectByExample(e1).get(0).getBrandname()+" ";
	    		di+=brandService.selectByExample(e).get(0).getBrandname()+" ";
	    		
	    	    model.addAttribute("che", di);
	    	}
	    	if(StringUtils.isNoneBlank(id)){
	    		Example e5 = new Example(Path.class);
	    		e5.createCriteria().andEqualTo("id", id);
	    		model.addAttribute("id", id);
	    		model.addAttribute("lei", pathService.selectByExample(e5).get(0).getPathname());
	    		CookieUtils.setCookie(response, "pathlei",pathService.selectByExample(e5).get(0).getId() );
	    		Example example = new Example(Path.class);
	    		example.createCriteria().andEqualTo("pid", id);
	    		List<Path> list = pathService.selectByExample(example);
	    		String branid = CookieUtils.getCookie(request, "Brand_session");
	    	
	    	    String html="";
	    		for(Path p :list){
	    			html+=getlist(p.getId(), branid);
	    		}
	    		
	    		
	    		
	    		model.addAttribute("html", html);
	    	
	    		
	    	}
	      
		}catch(Exception e){
			return citylist;
		}
	
	   
		return serlist;
	}
	
	@RequestMapping("citylist")
	public String citylist(Model model,HttpServletRequest request) {
		String city = CookieUtils.getCookie(request, "str");
		try{
			if(StringUtils.isBlank(city)){
				return citylist;
			}
			model.addAttribute("city", areaService.selectbyid(city).getAreaname());
		}catch(Exception e){
			e.printStackTrace();
		}
		return citylist;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, Model model) {
		String branid = CookieUtils.getCookie(request, "Brand_session");
		String city = CookieUtils.getCookie(request, "str");
	
		List<Path> list = null;
		try {
			if (StringUtils.isBlank(branid)) {
				return carlist;
			}
			if(city!=null){
				 if(city.indexOf("区")==-1){
					 model.addAttribute("city", areaService.selectbyid(city).getAreaname());
				 }
			}else{
				 model.addAttribute("city","" );
			}
	
		    
			Example e = new Example(Path.class);
			//e.createCriteria().andEqualTo("pid","402880ed4e006fd3014e006fd37e0000");
			e.createCriteria().andEqualTo("pid", "8a22bb414e70bd9b014e71bc30c10060");
			list = pathService.selectByExample(e);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("list", list);
	
		return index;
	}

	@RequestMapping(value = "setbrandbyid", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject setcookie(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		JSONObject json = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Brand b = null;
		try {
			if (StringUtils.isNoneBlank(id)) {
				b = brandService.selectByPrimaryKey(id);
				if (b != null) {
					map.put("success", true);
					CookieUtils.setCookie(response, "Brand_session", id);
					map.put("url", "/weixin/f/matenance/by/brand");
				}
			}
		} catch (Exception e) {
			map.put("message", "异常错误");
			map.put("success", false);
		}
		json.putAll(map);
		return json;
	}

	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON carlist(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		Example e = new Example(Brand.class);
		Criteria c = e.createCriteria();
		String id = request.getParameter("id");
		if (StringUtils.isNoneBlank(id)) {
			c.andEqualTo("pid", id).andEqualTo("state", 1);
		} else {
			c.andEqualTo("pid", "").andEqualTo("state", 1);
		}
		try {
			List<Brand> list = brandService.selectByExample(e);
			json.setRows(list);
			json.setSuccess(true);
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping(value = "cilist", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON clist(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		List<Area> list = null;
		try {
			String id = request.getParameter("id");

			Example e = new Example(Area.class);
			if (StringUtils.isBlank(id)) {
				e.createCriteria().andEqualTo("pid",
						"402880ed4bed4f92014bed4f92d50000");
			} else {
				e.createCriteria().andEqualTo("pid", id);
			}
			list = areaService.selectByExample(e);
			json.setRows(list);
			json.setSuccess(true);
		} catch (Exception e) {
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping(value="setcity",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON setcity(HttpServletRequest request,HttpServletResponse response){
		  CommonJSON json = new CommonJSON();
		  try{
			  String id = request.getParameter("cityid");
			  String cheng = request.getParameter("citycheng");
			  if(StringUtils.isNotBlank(id)){
				  CookieUtils.setCookie(response, "str", id);
				  if(StringUtils.isNoneBlank(cheng)){
					 CookieUtils.setCookie(response, "stor", cheng+id);
				  }else{
					  Area aid  = areaService.selectByPrimaryKey(id);
					  Area bid = areaService.selectByPrimaryKey(aid.getPid());
					  Area cid = areaService.selectByPrimaryKey(bid.getPid());
					  CookieUtils.setCookie(response, "stor", cid.getAreaname()+bid.getAreaname()+aid.getAreaname());
				  }
				  json.setSuccess(true);
			  }
		  }catch(Exception e){
			  json.setSuccess(false);
		  }
		  return json;
	}
	
	private String getlist(String pathid,String brandid){
		
		Example example = new Example(Matnproduct.class);
		example.createCriteria().andEqualTo("pathId", pathid)
		.andEqualTo("brandId", brandid);
		example.setOrderByClause("xprice asc");
		List<Matnproduct> prolist = null;
		try {
			 prolist = productService.selectByExample(example);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		String html="";
		if(prolist!=null && prolist.size()>0){
		    for(int i =0;i<prolist.size();i++){
		    	if(i==0){
		    		html+="<li>"+prolist.get(i).getProductname()+"<span class=\"price\">"+prolist.get(i).getXprice()+"元</span>"
		    			 +"<input type=\"hidden\" value=\""+prolist.get(i).getXprice()+"\" class=\"xp\"/>"
		    			 +"<input type=\"hidden\" value=\""+prolist.get(i).getProductid()+"\" class=\"id\" /> </li>"
						 +"<div class=\"other-selection\">"
						 +"<button class=\"btn btn-default\" value=\""+prolist.get(i).getProductname()+","+prolist.get(i).getXprice()+","+prolist.get(i).getProductid()+"\">"+prolist.get(i).getProductname()+" ("+prolist.get(i).getXprice()+"元)</button>";
		    	}else if(i==(prolist.size()-1)){
		    		html+="<button class=\"btn btn-default\" value=\""+prolist.get(i).getProductname()+","+prolist.get(i).getXprice()+","+prolist.get(i).getProductid()+"\">"+prolist.get(i).getProductname()+" ("+prolist.get(i).getXprice()+"元)</button></div>";
		    	}else{
		    		html+="<button class=\"btn btn-default\" value=\""+prolist.get(i).getProductname()+","+prolist.get(i).getXprice()+","+prolist.get(i).getProductid()+"\">"+prolist.get(i).getProductname()+" ("+prolist.get(i).getXprice()+"元)</button>";
		    	}
		    }
		}else{
			html+="<li>暂无产品!</li>";
		}
		return html;
	}
	
	@RequestMapping("erro")
	public String erro(){
		return ero;
	}

}
